//
//  BIDApplyReviewViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDApplyReviewViewController.h"
#import "BIDMemberListViewController.h"
#import "BIDApplyListViewController.h"
#import "BIDCustomTextView.h"

static NSString *reviewURL = @"requestAndroid!review.action";

@interface BIDApplyReviewViewController ()<BIDMemberListViewControllerDelegate>
{
    NSString *_reviewerID;
}
@end

@implementation BIDApplyReviewViewController
@synthesize requestID;
@synthesize requestReplyID;
@synthesize isAgree;
@synthesize isApply;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"审批";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendApply)];
    _textView.placeholder = @"请填写审批意见……";
    _textView.inputAccessoryView = self.toolBar;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendApply
{
    if(_reviewerID.length>0)
    {
        NSArray *arr = [_reviewerID componentsSeparatedByString:@","];
        if(arr && arr.count>1)
        {
            [self showAlertView:@"只能选择一个审批人" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
            return ;
        }
    }
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], reviewURL];
    NSMutableString *strPost  = [NSMutableString string];
    [strPost appendFormat:@"requestReply.id=%@", requestReplyID];
    [strPost appendFormat:@"&requestReply.isAgree=%@", isAgree?@0:@1];
    [strPost appendFormat:@"&reviewContent=%@", _textView.text];
    [strPost appendFormat:@"&requestReply.isFinish=%@", _reviewerID.length>0?@0:@1];
    [strPost appendFormat:@"&requestReply.requestId=%@", requestID];
    [strPost appendFormat:@"&requestReply.receiveUserId=%@", _reviewerID];
    [strPost appendFormat:@"&requestReply.type=%@", isApply?@0:@1];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSNumber *resValue = responseDic[@"isSuccess"];
            if([resValue intValue] == 1)
            {
                [self showAlertView:@"操作成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler:"];
            }
            else
            {
                [self showAlertView:@"操作失败" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
            }
        }
    }];
}

- (void)alertViewHandler:(id)obj
{
    NSArray *arr = self.navigationController.viewControllers;
    for(UIViewController *vc in arr)
    {
        if([vc isKindOfClass:[BIDApplyListViewController class]])
        {
            [self.navigationController popToViewController:vc animated:YES];
            break;
        }
    }
}

- (IBAction)chooseReviewerHandler:(id)sender
{
    BIDMemberListViewController *vc = [[BIDMemberListViewController alloc] init];
    vc.delegate = self;
    vc.chooseType = TYPE_CHECK;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - BIDMemberListViewControllerDelegate
- (void)getChoosedMember:(NSString *)ids andNames:(NSString *)names
{
    _reviewerTF.text = names;
    _reviewerID = ids;
}

@end
